package com.aw.guiguzi.adapter;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.aw.guiguzi.R;
import com.aw.guiguzi.bean.CalSuggestItemBean;
import com.aw.guiguzi.constants.MyConstant;
import com.aw.guiguzi.util.AppCacheUtil;
import com.aw.guiguzi.util.DataUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MainCalAnalaysisAdapter extends BaseQuickAdapter<CalSuggestItemBean, BaseViewHolder> {

    private List<CalSuggestItemBean> mList = new ArrayList<>();

    public MainCalAnalaysisAdapter() {
        super(R.layout.adapter_main_cal_analaysis);
        if(mList == null) {
            mList = new ArrayList<>();
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, final CalSuggestItemBean bean) {
        helper.setText(R.id.tv_type, bean.getType())
                .setText(R.id.tv_name, bean.getName() + "")
                .setText(R.id.tv_countAllJump, bean.getResultCount(bean.getCountAllTypeJump()))
                .setText(R.id.tv_countAllSort, bean.getResultCount(bean.getCountAllTypeSort()))
                .setText(R.id.tv_pre15Jump, bean.getResultCount(bean.getCountPre15Type1()))
                .setText(R.id.tv_pre15Sort, bean.getResultCount(bean.getCountPre15Type2()))
                .setText(R.id.tv_countOutPreJump, bean.getResultCount(bean.getCountPreTypeJump()))
                .setText(R.id.tv_countOutPreSort, bean.getResultCount(bean.getCountPreTypeSort()))
                .setText(R.id.tv_limitCountAllJump, bean.getResultCount(bean.getLimitCountAllTypeJump()))
                .setText(R.id.tv_limitCountAllSort, bean.getResultCount(bean.getLimitCountAllTypeSort()))
                .setText(R.id.tv_limitCountJump, bean.getResultCount(bean.getLimitCountTypeJump()))
                .setText(R.id.tv_limitCountSort, bean.getResultCount(bean.getLimitCountTypeSort()))
                .setText(R.id.tv_typeName1, bean.getTypeNameJump())
                .setText(R.id.tv_typeName2, bean.getTypeNameSort())
                .setText(R.id.tv_typeRate, bean.getTypeNameJump() + "(%)")
                .setText(R.id.tv_allRate, bean.getRateAll())
                .setText(R.id.tv_pre15Rate, bean.getPre15Rate())
                .setText(R.id.tv_outPreRate, bean.getRateOutPre())
                .setText(R.id.tv_limitAllRate, bean.getRateLimitAll())
                .setText(R.id.tv_limitRate, bean.getRateLimit())
                .setText(R.id.tv_screenOutTitle, "非前" +  AppCacheUtil.getAnalysisNumsScreenOutPreCustom() + "位")
                .setText(R.id.tv_limitAnalysisNumsLengthTitle, "后" +  AppCacheUtil.getAnalysisLengthCustom() + "位")
                .setText(R.id.tv_limitStatisticsCountNumsLengthTitle, "后" +  AppCacheUtil.getLimitStatisticsCountCustom() + "位")
                .setGone(R.id.tv_typeRate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_allRate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_pre15Rate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_outPreRate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_limitAllRate, AppCacheUtil.isUseTypePercent())
                .setGone(R.id.tv_limitRate, AppCacheUtil.isUseTypePercent())
                .setText(R.id.tv_value, bean.getStrValue());
        if(helper.getLayoutPosition() != 0) {
            if(bean.getType().equals(mList.get(helper.getLayoutPosition() - 1).getType())) {
                helper.setGone(R.id.tv_type, false)
                        .setGone(R.id.ll_title, false);
            } else {
                helper.setGone(R.id.tv_type, true)
                        .setGone(R.id.ll_title, AppCacheUtil.isStatisticsCount());
            }
        } else {
            helper.setGone(R.id.tv_type, true)
                    .setGone(R.id.ll_title, AppCacheUtil.isStatisticsCount());
        }
        helper.setGone(R.id.ll_content, AppCacheUtil.isStatisticsCount());

        LinearLayout ll_layout = helper.getView(R.id.ll_layout);

        TextView tv_type = helper.getView(R.id.tv_type);
        TextView tv_name = helper.getView(R.id.tv_name);
        TextView tv_value = helper.getView(R.id.tv_value);
        tv_type.setText(DataUtil.INSTANCE.matcherSearchText(mContext.getResources().getColor(R.color.black), bean.getType(), bean.getType(), true));
        tv_name.setText(DataUtil.INSTANCE.matcherSearchText(mContext.getResources().getColor(R.color.black), bean.getName(), bean.getName(), true));
        if(AppCacheUtil.getLimitType() == MyConstant.SWITCH_ALL) {
            tv_value.setText(DataUtil.INSTANCE.matcherSearchTextScreenOutPre(mContext.getResources().getColor(com.aw.baselib.R.color.red), bean.getStrValue(), new String[]{bean.getTypeNameJump(), "B"}));
        } else {
            tv_value.setText(DataUtil.INSTANCE.matcherSearchText(mContext.getResources().getColor(com.aw.baselib.R.color.red), bean.getStrValue(), new String[]{bean.getTypeNameJump(), "B"}, true));
        }

//        if(bean.isSuggestItem()) {
//            ll_layout.setBackgroundColor(mContext.getResources().getColor(com.aw.baselib.R.color.color_app_green));
//            helper.setTextColor(R.id.tv_type, mContext.getResources().getColor(R.color.white))
//                    .setTextColor(R.id.tv_name, mContext.getResources().getColor(R.color.white))
//                    .setTextColor(R.id.tv_value, mContext.getResources().getColor(R.color.white));
//            tv_type.setTypeface(null, Typeface.BOLD);
//            tv_name.setTypeface(null, Typeface.BOLD);
//            tv_value.setTypeface(null, Typeface.BOLD);
//        } else {
//            if(helper.getLayoutPosition()%2 == 0) {
//                ll_layout.setBackgroundColor(mContext.getResources().getColor(com.aw.baselib.R.color.color_item_1));
//            } else {
//                ll_layout.setBackgroundColor(mContext.getResources().getColor(com.aw.baselib.R.color.color_item_2));
//            }
//            helper.setTextColor(R.id.tv_type, mContext.getResources().getColor(R.color.black))
//                    .setTextColor(R.id.tv_value, mContext.getResources().getColor(R.color.black));
//            if(bean.getName().contains("跳")) {
//                helper.setTextColor(R.id.tv_name, mContext.getResources().getColor(com.aw.baselib.R.color.red));
//            } else {
//                helper.setTextColor(R.id.tv_name, mContext.getResources().getColor(com.aw.baselib.R.color.black));
//            }
//            tv_type.setTypeface(null, Typeface.NORMAL);
//            tv_name.setTypeface(null, Typeface.NORMAL);
//            tv_value.setTypeface(null, Typeface.NORMAL);
//        }
    }

    public void addAllData(List<CalSuggestItemBean> dataList) {
        this.mList = dataList;
        this.setNewData(mList);
        notifyDataSetChanged();
    }

    public void clearData() {
        if(mList != null) {
            int startPosition = 0;//hasHeader is 1
            int preSize = this.mList.size();
            if(preSize > 0) {
                this.mList.clear();
                notifyItemRangeRemoved(startPosition, preSize);
            }
        }
    }
}
